package com.jbit.userserver.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jbit.common.BusinessConstance;
import com.jbit.common.Constance;
import com.jbit.common.RedisEntity;
import com.jbit.redis.api.RedisAPI;
import com.jbit.userserver.model.entity.User;
import com.jbit.userserver.model.entity.UserBusiness;
import com.jbit.userserver.mapper.UserMapper;
import com.jbit.userserver.service.FunctionService;
import com.jbit.userserver.service.RoleService;
import com.jbit.userserver.service.UserBusinessService;
import com.jbit.userserver.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-12-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserBusinessService userBusinessService;
    @Resource
    private FunctionService functionService;
    @Resource
    private RoleService roleService;
    @Resource
    private RedisAPI redisAPI;
    @Override
    public Map<String, Object> login(String loginName, String password) {
        Map<String,Object> map = new HashMap();
        User user = userMapper.login(loginName);
        if (user == null){
            map.put("msgTip","user is not exist");
        }else{
            if (user.getStatus() != 0){
                map.put("msgTip","user is black");
            }else if(!user.getPassword().equals(password)){
                map.put("msgTip","user password error");
            }else{
                String uuid = UUID.randomUUID().toString();
                redisAPI.set(RedisEntity.send(uuid,user,1800));
                map.put("msgTip","user can login");
                //进行数据的查询
                UserBusiness userBusiness = userBusinessService.getBusinessByTypeAndKeyId(BusinessConstance.BUSINESS_USER_ROLE,user.getId());
                Integer roleId = Integer.parseInt(userBusiness.getValue().replace("[","").replace("]",""));
                UserBusiness btnBusiness = userBusinessService.getBusinessByTypeAndKeyId(BusinessConstance.BUSINESS_ROLE_FUNCTIONS,roleId);
                //查询按钮
                List<Map> userBtn = JSONObject.parseArray(btnBusiness.getBtnStr(),Map.class);
                for (Map btnMap : userBtn) {
                    String url = functionService.getUrlById(btnMap.get("funId").toString());
                    btnMap.remove("funId");
                    btnMap.put("url",url);
                }
                //查询角色类型
                String type = roleService.getTypeById(roleId);
                map.put("roleType",type);
                map.put("userBtn",userBtn);
                map.put("user",user);
                map.put("token",uuid);
            }
        }
        return map;
    }

    @Override
    public User getUserByToken(HttpServletRequest request) {
        String token = request.getHeader(Constance.TOKEN);
        return JSONObject.parseObject(redisAPI.get(RedisEntity.send(token)).toString(),User.class) ;
    }
}
